//
//  AppDelegate.m
//  ZSY_BreadTrip
//
//  Created by 张诗宇 on 15/12/28.
//  Copyright © 2015年 张诗宇. All rights reserved.
//

#import "AppDelegate.h"
#import "ZSY_HomeListViewController.h"
#import "ZSY_CityHunterListViewController.h"
#import "ZSY_DestinationListViewController.h"
#import "ZSY_UserViewController.h"
#import "UMSocial.h"
#import "ZWIntroductionViewController.h"

#import "ZSY_DataBaseManager.h"

@interface AppDelegate ()
@property (nonatomic, strong) ZWIntroductionViewController *introductionView;


@end

@implementation AppDelegate

- (void)dealloc{
    [_introductionView release];
    [_window release];
    [super dealloc];
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor colorWithRed:248 / 255.0 green:243 / 255.0 blue:233 / 255.0 alpha:1];
    [self.window makeKeyAndVisible];
    
    UITabBarController *tabBarC = [[UITabBarController alloc] init];
    tabBarC.tabBar.backgroundColor = [UIColor whiteColor];
    [tabBarC.tabBar setBackgroundImage:[UIImage imageNamed:@"account_uploadavatar_btn"]];

    
    // 设置AppKey
    [UMSocialData setAppKey:@"5694e85267e58e8e770009cb"];

    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstStart"]){
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstStart"];
        ZSY_HomeListViewController *view1 = [[ZSY_HomeListViewController alloc] init];
        self.window.rootViewController = view1;
       
        
        
        NSArray *coverImageNames = @[@"aaa", @"aaa", @"aaa"];
        NSArray *backgroundImageNames = @[@"lead1.jpg", @"lead2.jpg", @"lead3.jpg"];
        self.introductionView = [[ZWIntroductionViewController alloc] initWithCoverImageNames:coverImageNames backgroundImageNames:backgroundImageNames];
        
        // Example 2
        UIButton *enterButton = [UIButton new];
        enterButton.frame = CGRectMake(enterButton.frame.origin.x, enterButton.frame.origin.y, 100, 35);
        [enterButton setBackgroundImage:[UIImage imageNamed:@"toturial_view4_btn"] forState:UIControlStateNormal];
        self.introductionView = [[ZWIntroductionViewController alloc] initWithCoverImageNames:coverImageNames backgroundImageNames:backgroundImageNames button:enterButton];
        
        [view1.view addSubview:self.introductionView.view];
        
        //        __weak AppDelegate *weakSelf = self;
        self.introductionView.didSelectedEnter = ^() {
            [self.introductionView.view removeFromSuperview];
            self.introductionView = nil;
            
            // enter main view , write your code ...
            // 主页
            ZSY_HomeListViewController *homeListVC = [[ZSY_HomeListViewController alloc] init];
            UINavigationController *homeNaVC = [[UINavigationController alloc] initWithRootViewController:homeListVC];
            homeNaVC.navigationBar.tintColor = [UIColor whiteColor];
            [homeNaVC.navigationBar setBackgroundImage:[UIImage imageNamed:@"account_uploadavatar_btn"] forBarMetrics:UIBarMetricsDefault];
            //    UIImage *image = [UIImage imageNamed:@"home"];
            //    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            UIImage *homeImage = [UIImage imageNamed:@"iconfont-home"];
            homeListVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"主页" image:homeImage tag:10001];
            homeNaVC.navigationBar.translucent = NO;
            //    [homeListVC.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:] forBarMetrics:UIBarMetricsDefault];
            
            // 城市猎人
            ZSY_CityHunterListViewController *cityHunterListVC = [[ZSY_CityHunterListViewController alloc] init];
            UINavigationController *cityHunterNaVC = [[UINavigationController alloc] initWithRootViewController:cityHunterListVC];
            cityHunterNaVC.navigationBar.tintColor = [UIColor whiteColor];
            [cityHunterNaVC.navigationBar setBackgroundImage:[UIImage imageNamed:@"account_uploadavatar_btn"] forBarMetrics:UIBarMetricsDefault];
            UIImage *cityImage = [UIImage imageNamed:@"iconfont-cityHunter"];
            cityHunterListVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"城市猎人" image:cityImage tag:10002];
            cityHunterNaVC.navigationBar.translucent = NO;
            
            
            // 目的地
            ZSY_DestinationListViewController *destinationListVC = [[ZSY_DestinationListViewController alloc] init];
            UINavigationController *destinationNaVC = [[UINavigationController alloc] initWithRootViewController:destinationListVC];
            destinationNaVC.navigationBar.tintColor = [UIColor whiteColor];
            [destinationNaVC.navigationBar setBackgroundImage:[UIImage imageNamed:@"account_uploadavatar_btn"] forBarMetrics:UIBarMetricsDefault];
            UIImage *deatinationImage = [UIImage imageNamed:@"iconfont-destination"];
            destinationListVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"目的地" image:deatinationImage tag:10003];
            destinationNaVC.navigationBar.translucent = NO;
            
            
            // 我的
            ZSY_UserViewController *userVC = [[ZSY_UserViewController alloc] init];
            UINavigationController *userNaVC = [[UINavigationController alloc] initWithRootViewController:userVC];
            [userNaVC.navigationBar setBackgroundImage:[UIImage imageNamed:@"account_uploadavatar_btn"] forBarMetrics:UIBarMetricsDefault];
            userNaVC.navigationBar.backgroundColor = [UIColor whiteColor];
            UIImage *userIamge = [UIImage imageNamed:@"iconfont-user"];
            userVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:userIamge tag:10004];
            
            
            
            
            tabBarC.viewControllers = [NSArray arrayWithObjects:homeNaVC,cityHunterNaVC, destinationNaVC, userNaVC, nil];
            tabBarC.tabBar.translucent = NO;
            // tabBarC的背景色
            tabBarC.tabBar.backgroundColor = [UIColor whiteColor];
            // tabBarC按钮选中颜色
            tabBarC.tabBar.tintColor = [UIColor lightGrayColor];
            self.window.rootViewController = tabBarC;
            
            
            [_window release];
            [tabBarC release];
            [homeNaVC release];
            [homeListVC release];

        };
    } else {
        //        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstStart"];
        //        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstStart"];

        
    // 主页
    ZSY_HomeListViewController *homeListVC = [[ZSY_HomeListViewController alloc] init];
    UINavigationController *homeNaVC = [[UINavigationController alloc] initWithRootViewController:homeListVC];
    homeNaVC.navigationBar.tintColor = [UIColor whiteColor];
    [homeNaVC.navigationBar setBackgroundImage:[UIImage imageNamed:@"account_uploadavatar_btn"] forBarMetrics:UIBarMetricsDefault];
//    UIImage *image = [UIImage imageNamed:@"home"];
//    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *homeImage = [UIImage imageNamed:@"iconfont-home"];
    homeListVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"主页" image:homeImage tag:10001];
    homeNaVC.navigationBar.translucent = NO;
//    [homeListVC.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:] forBarMetrics:UIBarMetricsDefault];
    
    // 城市猎人
    ZSY_CityHunterListViewController *cityHunterListVC = [[ZSY_CityHunterListViewController alloc] init];
    UINavigationController *cityHunterNaVC = [[UINavigationController alloc] initWithRootViewController:cityHunterListVC];
    cityHunterNaVC.navigationBar.tintColor = [UIColor whiteColor];
    [cityHunterNaVC.navigationBar setBackgroundImage:[UIImage imageNamed:@"account_uploadavatar_btn"] forBarMetrics:UIBarMetricsDefault];
    UIImage *cityImage = [UIImage imageNamed:@"iconfont-cityHunter"] ;
    cityHunterListVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"城市猎人" image:cityImage tag:10002];
    cityHunterNaVC.navigationBar.translucent = NO;
    
    
    // 目的地
    ZSY_DestinationListViewController *destinationListVC = [[ZSY_DestinationListViewController alloc] init];
    UINavigationController *destinationNaVC = [[UINavigationController alloc] initWithRootViewController:destinationListVC];
    destinationNaVC.navigationBar.tintColor = [UIColor whiteColor];
    [destinationNaVC.navigationBar setBackgroundImage:[UIImage imageNamed:@"account_uploadavatar_btn"] forBarMetrics:UIBarMetricsDefault];
    UIImage *deatinationImage = [UIImage imageNamed:@"iconfont-destination"] ;
    destinationListVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"目的地" image:deatinationImage tag:10003];
    destinationNaVC.navigationBar.translucent = NO;
    
    
    // 我的
    ZSY_UserViewController *userVC = [[ZSY_UserViewController alloc] init];
    UINavigationController *userNaVC = [[UINavigationController alloc] initWithRootViewController:userVC];
    [userNaVC.navigationBar setBackgroundImage:[UIImage imageNamed:@"account_uploadavatar_btn"] forBarMetrics:UIBarMetricsDefault];
    userNaVC.navigationBar.backgroundColor = [UIColor whiteColor];
    UIImage *userIamge = [UIImage imageNamed:@"iconfont-user"];
    userVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:userIamge tag:10004];
    
    
    
    
    tabBarC.viewControllers = [NSArray arrayWithObjects:homeNaVC,cityHunterNaVC, destinationNaVC, userNaVC, nil];
    tabBarC.tabBar.translucent = NO;
    // tabBarC的背景色
    tabBarC.tabBar.backgroundColor = [UIColor lightGrayColor];
    // tabBarC按钮选中颜色
    tabBarC.tabBar.tintColor = [UIColor whiteColor];
    self.window.rootViewController = tabBarC;
    
    
    [_window release];
    [tabBarC release];
    [homeNaVC release];
    [homeListVC release];
    }
    [[ZSY_DataBaseManager shareDataBase] openDB];
    [[ZSY_DataBaseManager shareDataBase] createCityTable];
    
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
